﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AAA.Base.Util.Pattern
{
    public class DefaultObservable : IObservable
    {
        private List<IObserver> _lstObserver;

        public DefaultObservable()
        {
            _lstObserver = new List<IObserver>();
        }    
#region IObservable Members
        public void AddObserver(IObserver observer)
        {
            if (_lstObserver.Contains(observer))
                return;

            _lstObserver.Add(observer);
        }

        public void  RemoveObserver(IObserver observer)
        {
            if (!_lstObserver.Contains(observer))
                return;

            _lstObserver.RemoveAt(_lstObserver.IndexOf(observer));
        }

        public void  RemoveAll()
        {
            while (_lstObserver.Count > 0)
                _lstObserver.RemoveAt(0);
        }

        public void  Notify(object oMessage)
        {
            foreach (IObserver observer in _lstObserver)
                observer.Update(this, oMessage);
        }

#endregion
}
}
